package com.zlm.log.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.*;

/**
 * Author: Harbour
 * Date: 2021-07-06 17:23
 * Desc:
 */

@RestController
@Slf4j
public class LogController {

    // spring 集成的kafka， 不用再自己写kafka producer
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @RequestMapping("/applog")
    public String applog(@RequestBody String mockLog) {
        JSONObject jsonObject = JSON.parseObject(mockLog);
        if (jsonObject.getJSONObject("start") != null) {
            kafkaTemplate.send("mall-start", mockLog);
        } else {
            kafkaTemplate.send("mall-event", mockLog);
        }
        log.info(mockLog);
        return "success";
    }

    @RequestMapping("/test")
    public String test(@RequestParam("name") String name, @RequestParam("age") String age) {
        return name + "已经" + age + "岁了！";
    }

    @RequestMapping("/test1")
    public String test() {
        return "test1";
    }
}




